﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using ECCIT;
using ECCIT.Data;
using OgilvyOne.VW.Beetle.Model;
using OgilvyOne.VW.Beetle.IDAL;

namespace OgilvyOne.VW.Beetle.DAL
{
    public class LikeCountDAL : IDALLikeCount
    {
        private const string C_TABLE_NAME = "tbl_LikeCount";
        private const string C_COL_LAND_ID = "col_LangID";
        private const string C_COL_ID = "col_ID";
        private const string C_COL_COUNT = "col_Count";

        private static readonly string S_SQL_INCREMENT_COUNT = RetrieveIncrementCountSql();
        private static readonly string S_SQL_GET_MAX_COUNT = RetrieveGetMaxCountSql();
        private static readonly string S_SQL_GET_MAX_COUNTS = RetrieveGetMaxCountsSql();

        private static string RetrieveIncrementCountSql()
        {
            return string.Format("update {0} set {1}={1}+1 where {2}=@{2} and {3}=@{3} "
            , C_TABLE_NAME, C_COL_COUNT, C_COL_LAND_ID, C_COL_ID);
        }

        private static string RetrieveGetMaxCountSql()
        {
            return string.Format("select {0} from {1} where {2}=@{2} and {3}=@{3}", C_COL_COUNT, C_TABLE_NAME, C_COL_LAND_ID, C_COL_ID);
        }

        private static string RetrieveGetMaxCountsSql()
        {
            return string.Format("select * from {0} where {1}=@{1} ", C_TABLE_NAME, C_COL_LAND_ID);
        }

        public int? LanguageID { get; set; }

        public LikeCountDAL(int? langID)
        {
            this.LanguageID = langID;
        }

        #region IDALLikeCount 成员

        public ResultInfo Increment(int? id)
        {
            ResultInfo result = new ResultInfo(false);
            if (!LanguageID.HasValue) return result;
            if (!id.HasValue) return result;

            try
            {
                IDataParameter[] parms = new SqlParameter[] {
                    new SqlParameter("@" + C_COL_LAND_ID, SqlDbType.Int),
                    new SqlParameter("@" + C_COL_ID, SqlDbType.Int)
                };

                parms[0].Value = LanguageID.Value;
                parms[1].Value = id.Value;

                int rowsAffected = DBHelper.ExecuteNonQuery(S_SQL_INCREMENT_COUNT, parms);
                if (rowsAffected > 0)
                {
                    ResultInfo getRes = GetMaxCount(id);
                    if (!getRes.Result) throw getRes.Exception;
                    result.ReturnObject = getRes.ReturnObject;
                    result.Result = true;
                }
            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Message = "Error occurred in LikeCountDAL.Increment(). Message: " + ex.Message;
            }

            return result;
        }

        public ResultInfo GetMaxCount(int? id)
        {
            ResultInfo result = new ResultInfo(false);
            if (!LanguageID.HasValue) return result;
            if (!id.HasValue) return result;

            try
            {
                IDataParameter[] parms = new SqlParameter[] {
                    new SqlParameter("@" + C_COL_LAND_ID, SqlDbType.Int),
                    new SqlParameter("@" + C_COL_ID, SqlDbType.Int)
                };

                parms[0].Value = LanguageID.Value;
                parms[1].Value = id.Value;

                object obj = DBHelper.ExecuteScalar(S_SQL_GET_MAX_COUNT, parms);
                if (obj != null && obj != DBNull.Value)
                {
                    result.ReturnObject = obj;
                    result.Result = true;
                }
            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Message = "Error occurred in LikeCountDAL.GetMaxCount(). Message: " + ex.Message;
                result.Exception = ex;
            }

            return result;
        }

        public ResultInfo GetModels()
        {
            ResultInfo result = new ResultInfo(false);
            if (!LanguageID.HasValue) return result;

            try
            {
                IDataParameter[] parms = new SqlParameter[] {
                    new SqlParameter("@" + C_COL_LAND_ID, SqlDbType.Int)
                };

                parms[0].Value = LanguageID.Value;

                List<LikeCountModel> models = new List<LikeCountModel>();
                using (IDataReader reader = DBHelper.ExecuteReader(S_SQL_GET_MAX_COUNTS, parms))
                {
                    while (reader.Read())
                        models.Add(new LikeCountModel((int?)reader[C_COL_LAND_ID], (int?)reader[C_COL_ID], (long?)reader[C_COL_COUNT]));
                }

                result.ReturnObject = models;
                result.Result = true;
            }
            catch (Exception ex)
            {
                result.Result = false;
                result.Message = "Error occurred in LikeCountDAL.GetModels(). Message: " + ex.Message;
                result.Exception = ex;
            }

            return result;
        }

        #endregion
    }
}